package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.DateUtils;
import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.library.util.MsgType;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.WNXBean;

/**
 * Created by Administrator on 2016/10/18 0018.
 */
public class NoticeOrWNXAdapter extends BaseAdapter {
    private static final String TAG = "MessageListAdapter";
    private  Context mContext;
    private List<WNXBean> dataList;
    private static String user_logo = "";


    public NoticeOrWNXAdapter(Context context, List<WNXBean> dataList) {
        super();
        this.mContext = context;
        user_logo = MyInfoUtil.getMyLogo(mContext);
        this.dataList = dataList;
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    @Override
    public boolean isEmpty() {
        return super.isEmpty();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        MessageViewHolder viewHolder;
        WNXBean wnxBean = dataList.get(i);
        if (convertView == null) {
            viewHolder = new MessageViewHolder();

//            if (wnxBean.msg_type == MsgType.WNX_MSG.toNumber()) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_message, null);
//            } else {
//                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_message_notice, null);
//            }

            viewHolder.findView(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MessageViewHolder) convertView.getTag();
        }
        viewHolder.setData(viewHolder, wnxBean);
        return convertView;
    }

    /**
     * 局部刷新
     *
     * @param view
     * @param itemIndex
     */
    public void updateItem(View view, int itemIndex) {
        if (view == null) {
            return;
        }
        //从view中取得holder
        MessageViewHolder holder = (MessageViewHolder) view.getTag();
        holder.setData(holder, getItem(itemIndex));
    }

    @Override
    public long getItemId(int i) {
        return dataList.get(i).hashCode();
    }

    @Override
    public WNXBean getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    private class MessageViewHolder {
        ImageView logoIv;
        TextView unreadTv;
        TextView titleTv;
        TextView contentTv;
        TextView timeTv;
        RelativeLayout relative_bg;

        public void findView(View v) {
            this.logoIv = (RoundAngleImageView) v.findViewById(R.id.imageview_head);
            this.unreadTv = (TextView) v.findViewById(R.id.tv_unreadcount);
            this.titleTv = (TextView) v.findViewById(R.id.tv_title);
            this.contentTv = (TextView) v.findViewById(R.id.tv_content);
            this.timeTv = (TextView) v.findViewById(R.id.tv_time);
            this.relative_bg = (RelativeLayout) v.findViewById(R.id.relative_bg);
        }

        public void setData(MessageViewHolder viewHolder, WNXBean wnxBean) {
            try {
//                viewHolder.logoIv.setImageBitmap(BitmapUtil.getBitmapByBase64(messageEntity.msg_logo));
                HeadShowUtil.headShow(mContext, wnxBean.msg_logo, "", viewHolder.logoIv, HeadShowUtil.HeadType.OrgLogo);
            } catch (Exception e) {
                e.printStackTrace();
            }

//            if (!TextUtils.isEmpty(messageEntity.msg_logo)) {
//                Picasso.with(mContext)
//                        .load(messageEntity.msg_logo)
//                        .placeholder(R.drawable.icon_org_logo_default)
//                        .error(R.drawable.icon_org_logo_default)
//                        .into(viewHolder.logoIv);
//            } else {
//                viewHolder.logoIv.setImageResource(R.drawable.icon_org_logo_default);
//            }

//            if (wnxBean.msg_type == MsgType.WNX_MSG.toNumber()) {
//                viewHolder.unreadTv.setVisibility(View.VISIBLE);
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2); //wrap_content
//                layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;
//                unreadTv.setLayoutParams(layoutParams);
//                if (wnxBean.unreadCount > 99) {
//                    unreadTv.setText("...");
//                } else {
//                    unreadTv.setText(String.valueOf(wnxBean.unreadCount));
//                }
//            } else {//通知公告的直接显示小红点
//                if (wnxBean.msg_unread_flag == 0) {
//                    //未读
//                    unreadTv.setVisibility(View.VISIBLE);
//                } else {
//                    //已读
//                    unreadTv.setVisibility(View.GONE);
//                }
//                unreadTv.setText("");
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(viewHolder.unreadTv
//                        .getLayoutParams());
//                layoutParams.width = 30;
//                layoutParams.height = 30;
//                layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;
//                unreadTv.setLayoutParams(layoutParams);
//            }

            if (wnxBean.msg_type == MsgType.WNX_MSG.toNumber()) {
                titleTv.setText(wnxBean.msg_sender_name);
                contentTv.setText(wnxBean.msg_title);
                if (wnxBean.unreadCount == 0) {
                    unreadTv.setVisibility(View.GONE);
                } else {
                    unreadTv.setVisibility(View.VISIBLE);
                    if (wnxBean.unreadCount > 99) {
                        unreadTv.setText("...");
                    } else {
                        unreadTv.setText(wnxBean.unreadCount + "");
                    }
                }
            } else {
                titleTv.setText(wnxBean.msg_title);
                contentTv.setText(wnxBean.msg_sender_name);
                if (wnxBean.msg_unread_flag == 0) {
                    //未读
                    unreadTv.setVisibility(View.VISIBLE);
                } else {
                    //已读
                    unreadTv.setVisibility(View.GONE);
                }
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(viewHolder.unreadTv
                        .getLayoutParams());
                layoutParams.width = 30;
                layoutParams.height = 30;
                layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;
                unreadTv.setLayoutParams(layoutParams);
            }
            if (!TextUtils.isEmpty(wnxBean.msg_send_time)) { //1为列表没有时间
                viewHolder.timeTv.setText(DateUtils.getMessageList(wnxBean.msg_send_time, DateUtils.FORMAT_1));
            } else {
                viewHolder.timeTv.setText("");
            }
        }
    }
}
